<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP '301.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script language = "javascript">
		function checkNum(){
			if(document.form1.num1.value == ""){
				return false;
			}
			if(Math.round(document.form1.num1.value) != document.form1.num1.value){
				alert("输入不正确");
				return false;
			}		
			if(Math.round(document.form1.num2.value) != document.form1.num2.value){
				alert("输入不正确");
				return false;
			}
			if(document.form1.operator.value == "/" && document.form1.num2.value == 0){
				alert("除数不能为0");
				return false;
			}
			
			
		}
	</script>

  </head>
  
    <body>
    <h1>我的计算器</h1>
	<hr>
	<%
				String strNum1 = request.getParameter("num1");
				String strNum2 = request.getParameter("num2");
				String operator = request.getParameter("operator");
				int num11=0, num22=0,result=0;
				if(strNum1 != null && strNum2 != null && operator != null){
	%>
				
				<%
				try{
					
					num11 = Integer.parseInt(strNum1);
					num22 = Integer.parseInt(strNum2);
					
					if(operator.equals("+")){
						result = num11 + num22;
					}else if(operator.equals("-")){
						result = num11 - num22;
					}else if(operator.equals("*")){
						result = num11 * num22;
					}else if(operator.equals("/")){
						result = num11/num22;
					}
				
				}catch(Exception e){
					e.printStackTrace();
					out.println("12345678");
				}	
				}
			 
			  %>
			
		<form name = "form1" action="301.jsp">
		请输入第一个数：<input type="text" name="num1" value="<%=strNum1 %>"><br>
		请选择运算方式：<select name = "operator">
			<option value=+>+</option>
			<option value=->-</option>
			<option value=*>*</option>
			<option value=/>/</option>
		</select><br>
		请输入第二个数：<input type="text" name="num2"  value="<%=strNum2 %>"><br>
		<input type=submit onclick="return checkNum()" value="等于">
		<input type="reset" name="form1" id="form1" value="重置"><br>
		结果：<input type=text value=<%=result %>>
		</form>
  </body>
</html>
